﻿using System;
using System.Collections.ObjectModel;
using System.Data.SqlClient;
using System.Linq;
using DataAccess;
using Entities;
namespace Business
{
    public class SupportMaterialBusiness
    {

        public static SupportMaterialBusiness Instance { get; private set; }


        static SupportMaterialBusiness() { Instance = new SupportMaterialBusiness(); }


        public string Update(SupportingMaterial supportingMaterial)
        {
            if (supportingMaterial == null)
            {

                return "supportingMaterial is not null";
            }
            try
            {
                return SupportingMaterialDataAccess.Instance.UpdateSupportingMaterial(supportingMaterial) > 0 ? "Update Successfull!" : "Update Fail";
            }
            catch (SqlException exception)
            {

                return exception.Message;
            }

        }

        public Collection<SupportingMaterial> PagingSupportingMaterial(int pageNumber, int pageSize, Collection<SupportingMaterial> lstMaterial)
        {

            if (lstMaterial != null)
            {
                if (pageSize > 0)
                {
                    var totalpage = lstMaterial.Count % pageSize == 0 ? lstMaterial.Count / pageSize : lstMaterial.Count / pageSize + 1;
                    if (pageNumber <= totalpage && pageNumber > 0)

                        return new Collection<SupportingMaterial>(lstMaterial.Skip((pageNumber - 1) * pageSize).Take(pageSize).ToList());

                }
                return null;
            }
            return null;
        }
        public SupportingMaterial GetSupportMaterial(string supportingMaterialId)
        {
            try
            {
                if (!string.IsNullOrEmpty(supportingMaterialId) && supportingMaterialId != Guid.Empty.ToString())
                    return SupportingMaterialDataAccess.Instance.GetSupportingMaterial(supportingMaterialId);
                return null;
            }
            catch (SqlException exception)
            {


                return null;
            }

        }
        public Collection<SupportingMaterial> ListSupportMaterial(string organisationId, bool isActive)
        {
            try
            {
                if (!string.IsNullOrEmpty(organisationId) && organisationId != Guid.Empty.ToString())
                    return SupportingMaterialDataAccess.Instance.ListSupportingMaterial(organisationId, isActive);
                return null;
            }
            catch (SqlException exception)
            {


                return null;
            }

        }
    }
}
